/*
	form元素添加data-do="verifyInput"则自动执行对应input type的监测
	失焦监测:
		1.type=tele:验证手机号码11位
		2.type=password:密码6位非空格符
		3.type=email 及 data-type=verify: 非空则去除警告
	点击监测:
		4.type=submit:点击提交时监测所有输入框是否空
	PS:mobile浏览器: type=email,会自动监测格式; type=tele,会自动监测数字
*/

(function ($, window) {
    
    $.fn.verifyInput = function () {
        var $thisForm = this;
        //电话码
        $thisForm.on("blur", "input[type=tel]", function () {
            var $this = $(this);
            var $father = $this.parents(".form-group");
            var $hint = $father.find(".hint");
            var val = $this.val();
            var len = val.length;
            if (len === 0) { return; }
            if (len !== 11 || val.match(/\D/)) {
                //非11位数字
                $father.addClass("has-error").removeClass("has-success");
                $hint.removeClass("hidden");
            }
            else {
                //正确
                $father.removeClass("has-error").addClass("has-success");
                $hint.addClass("hidden");
            }
        });
        //密码
        $thisForm.on("blur", "input[type=password]", function () {
            var $this = $(this);
            var $father = $this.parents(".form-group");
            var $hint = $father.find(".hint");
            var $formpw = $father.parents("form").find("input[type=password]");
            var val = $this.val();
            var len = val.length;
            var $pwType = $this.data("pwtype");

            //用于二次输入验证
            var new_val,
                new_rep_val,
                $new_box,
                $new_rep_box;

            //常规输入,或设置新密码的第一次输入
            if ($pwType !== "confirm") {
                if (len === 0) { return; }
                if (len < 6 || len > 20 || val.match(/\s/)) {
                    //只排除了空格,和非法长度
                    $father.addClass("has-error").removeClass("has-success");
                    $hint.removeClass("hidden");
                }
                else {
                    //正确
                    $father.removeClass("has-error").addClass("has-success");
                    $hint.addClass("hidden");
                }
            }
           
            //如果是设置密码,有两次输入
            if (($pwType === "confirm" || $pwType ==="first") && $formpw.length === 2) {
                var $new = $formpw.eq(0);
                var $new_rep = $formpw.eq(1);
                new_val = $new.val();
                $new_box = $new.parents(".form-group");
                new_rep_val = $new_rep.val();
                $new_rep_box = $new_rep.parents(".form-group");
                $hint2 = $new_rep_box.find(".hint");
               
                if (new_val !== new_rep_val && !($pwType === "first" && new_rep_val === "")) {
                    $new_box.addClass("has-error").removeClass("has-success");
                    $new_rep_box.addClass("has-error").removeClass("has-success");
                    $hint2.removeClass("hidden");
                } else if (new_rep_val !== "") {
                    $new_box.removeClass("has-error").addClass("has-success");
                    $new_rep_box.removeClass("has-error").addClass("has-success");
                    $hint2.addClass("hidden");
                }
            }
            
        });
        //验证码
        $thisForm.on("blur", "input[data-type=verify]", function () {
            var $this = $(this);
            var $father = $this.parents(".form-group");
            var $hint = $father.find(".hint");
            var val = $this.val();
            if (val !== "") {
                //非空
                $father.removeClass("has-error");
                $hint.addClass("hidden");
            }
        });
        //邮箱
        $thisForm.on("blur", "input[type=email]", function () {
            var $this = $(this);
            var $father = $this.parents(".form-group");
            var $hint = $father.find(".hint");
            var val = $this.val();
            if (val !== "") {
                //非空
                $father.removeClass("has-error");
                $hint.addClass("hidden");
            }
        });
        //点击提交
        $thisForm.on("click", "[type=submit]", function (e) {
            var $form = $(this).parents("form");
            //检查是否有没输入的
            $form.find("input").each(function (i, item) {
                var $input = $(item);
                var $father = $input.parents(".form-group");
                if ($input.val() === "") {
                    $father.addClass("has-error");
                    $father.find(".hint").removeClass("hidden");
                }
            });
            //检查到不符合的,则阻止提交表单:
            if ($form.find(".has-error").length > 0) {
                e.preventDefault();
                return;
            }
        });
    }

    $(document).ready(function ($) {
        //init:
        $("form[data-do=verifyInput]").verifyInput();
    });

})(Zepto, window);

